import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:example/utils/dialog_ext.dart';
import '../../base/custom_dialog.dart';
import '../../config/color_config.dart';
import '../../utils/common.dart';
import '../my_image.dart';

/// 时间区间选择器
class DateTimeRangeDialog extends CustomDialog<DateTimeDialogController> {
  DateFormat dateFormat = DateFormat("yyyy年MM月dd日");

  DateTimeRangeDialog({super.key,
    DateTimeDialogController? controller,
    String? title,
    int? type = PickerDateTimeType.kYMD,
    List<int>? customColumnType,
    DateTimePickerAdapter? startAdapter,
    DateTimePickerAdapter? endAdapter,
    DateTime? currentDate,
    DateTime? maxDate,
    DateTime? minDate,
    Function(DateTimeRangeDialog dialog, String date)? confirm,
  }) : super(
            DateTimeDialogController(
              confirm: confirm,
              title: title,
              startAdapter: startAdapter,
              type: type,
              customColumnType: customColumnType,
              currentDate: currentDate,
              maxDate: maxDate,
              minDate: minDate,
            ),
            externalController: controller) {
    if (controller != null) {
      controller.confirm = controller.confirm ?? confirm;
      controller.title = controller.title ?? title;
      controller.startAdapter = controller.startAdapter ?? startAdapter;
      controller.endAdapter = controller.endAdapter ?? endAdapter;
      controller.type = controller.type ?? type;
      controller.customColumnType =
          controller.customColumnType ?? customColumnType;
      controller.currentDate = controller.currentDate ?? currentDate;
      controller.maxDate = controller.maxDate ?? maxDate;
      controller.minDate = controller.minDate ?? minDate;
    }
  }

  @override
  Widget initView(BuildContext context, DateTimeDialogController controller) {
    return Stack(
      children: [
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12.r),
                topRight: Radius.circular(12.r),
              ),
            ),
            child: SafeArea(
              top: false,
              child: _body(context, controller),
            ),
          ),
        ),
      ],
    );
  }

  _body(BuildContext context, DateTimeDialogController controller) {
    return Column(
      children: [
        _title(controller),
        _content(controller),
      ],
    );
  }

  _title(DateTimeDialogController controller) {
    return SizedBox(
      height: 54.w,
      child: Row(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              hide();
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.fromLTRB(15.w, 16.w, 26.w, 16.w),
              child: MyImage(
                path: "assets/svg/ic_close.svg",
                width: 12.w,
                color: ColorConfig.FFBCB6B0,
              ),
            ),
          ),
          Expanded(
            child: Text(
              controller.title ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: ColorConfig.text,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              DateTime start=DateTime.parse(controller.startAdapter!.text);
              DateTime end=DateTime.parse(controller.endAdapter!.text);
              String date =
                  "${dateFormat.format(start)}-${dateFormat.format(end)}";
              controller.confirm?.call(this, date);
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              child: Text(
                "确定",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.dark_app_main,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _content(DateTimeDialogController controller) {
    return Row(
      children: [
        Expanded(
            child: Picker(
                hideHeader: true,
                height: 200.w,
                itemExtent: 40.w,
                selectionOverlay: Container(
                  margin: EdgeInsets.symmetric(horizontal: 8.w),
                  decoration: BoxDecoration(
                    color: ColorConfig.black.withOpacity(0.05),
                    borderRadius: BorderRadius.circular(10.r),
                  ),
                ),
                textStyle: TextStyle(
                    color: ColorConfig.FF020306,
                    fontSize: 13.sp,
                    fontWeight: FontWeight.w400),
                selectedTextStyle: TextStyle(
                  color: ColorConfig.text,
                  fontSize: 18.sp,
                ),
                adapter: controller.startAdapter!,
                title: Text(controller.title ?? ""),
                onConfirm: (Picker picker, List value) {
                  logger.i((picker.adapter as DateTimePickerAdapter).value);
                }).makePicker()),
        const Text("至"),
        Expanded(
            child: Picker(
                hideHeader: true,
                height: 200.w,
                itemExtent: 40.w,
                selectionOverlay: Container(
                  margin: EdgeInsets.symmetric(horizontal: 8.w),
                  decoration: BoxDecoration(
                    color: ColorConfig.black.withOpacity(0.05),
                    borderRadius: BorderRadius.circular(10.r),
                  ),
                ),
                textStyle: TextStyle(
                    color: ColorConfig.FF020306,
                    fontSize: 13.sp,
                    fontWeight: FontWeight.w400),
                selectedTextStyle: TextStyle(
                  color: ColorConfig.text,
                  fontSize: 18.sp,
                ),
                adapter: controller.endAdapter!,
                title: Text(controller.title ?? ""),
                onConfirm: (Picker picker, List value) {
                  logger.i((picker.adapter as DateTimePickerAdapter).value);
                }).makePicker()),
      ],
    );
  }
}

class DateTimeDialogController extends GetxController {
  DateTimePickerAdapter? startAdapter;
  DateTimePickerAdapter? endAdapter;
  String? title;
  Function(DateTimeRangeDialog dialog, String date)? confirm;
  List<int>? customColumnType;
  int? type;
  DateTime? maxDate;
  DateTime? minDate;
  DateTime? currentDate;
  final kToday = DateTime.now();

  DateTimeDialogController({
    this.confirm,
    this.title,
    this.startAdapter,
    this.endAdapter,
    this.type,
    this.customColumnType,
    this.currentDate,
    this.maxDate,
    this.minDate,
  }) {
    startAdapter ??= DateTimePickerAdapter(
      type: type ?? PickerDateTimeType.kYMD,
      customColumnType: customColumnType,
      value: currentDate,
      maxValue:
          maxDate ?? DateTime(kToday.year + 60, kToday.month, kToday.day),
      minValue:
          minDate ?? DateTime(kToday.year - 60, kToday.month, kToday.day),
      isNumberMonth: true,
      yearSuffix: "年",
      monthSuffix: "月",
      daySuffix: "日",
    );
    endAdapter ??= DateTimePickerAdapter(
      type: type ?? PickerDateTimeType.kYMD,
      customColumnType: customColumnType,
      value: currentDate,
      maxValue:
          maxDate ?? DateTime(kToday.year + 60, kToday.month, kToday.day),
      minValue:
          minDate ?? DateTime(kToday.year - 60, kToday.month, kToday.day),
      isNumberMonth: true,
      yearSuffix: "年",
      monthSuffix: "月",
      daySuffix: "日",
    );
  }
}
